﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LineWatch.Common;
using DBAccessLib;
using System.Globalization;
using System.IO;

namespace LineWatch_WinForm
{
    public partial class ProductManagementView : Form
    {
        private const string COMMAND_HEADER = "@00RD";
        private const string WRITE_COMMAND_HEADER = "@00WD";

        ScreenSaver screenSaver = null;

        private UserAndLineCodeModel userAndLineCodeModel = new UserAndLineCodeModel();

        private Dictionary<string, string> serialPortsAndLineCodes = new Dictionary<string, string>();
        private Dictionary<string, SerialPortOperator> lineCodesAndSerialPortOperators
                                                       = new Dictionary<string, SerialPortOperator>();

        private SerialPortOperator serialPortOperator1 = null;
        private SerialPortOperator serialPortOperator2 = null;
        private SerialPortOperator serialPortOperator3 = null;

        private string planNumPositionLine1 = string.Empty;
        private string nowPlanPositionLine1 = string.Empty;
        private string productNumPositionLine1 = string.Empty;
        private string stopTimePositionLine1 = string.Empty;
        private string ExcepDMPositionLine1 = string.Empty;
        private string StopPlanDMPositionLine1 = string.Empty;
        private DateTime currentNowPlanUpdatedTimeLine1 = DateTime.Now;

        private string planNumPositionLine2 = string.Empty;
        private string nowPlanPositionLine2 = string.Empty;
        private string productNumPositionLine2 = string.Empty;
        private string stopTimePositionLine2 = string.Empty;
        private string ExcepDMPositionLine2 = string.Empty;
        private string StopPlanDMPositionLine2 = string.Empty;
        private DateTime currentNowPlanUpdatedTimeLine2 = DateTime.Now;

        private string planNumPositionLine3 = string.Empty;
        private string nowPlanPositionLine3 = string.Empty;
        private string productNumPositionLine3 = string.Empty;
        private string stopTimePositionLine3 = string.Empty;
        private string ExcepDMPositionLine3 = string.Empty;
        private string StopPlanDMPositionLine3 = string.Empty;
        private DateTime currentNowPlanUpdatedTimeLine3 = DateTime.Now;

        private PositionCommandType currentPositionCommandTypeLine1 = PositionCommandType.AllProductData;
        private PositionCommandType currentPositionCommandTypeLine2 = PositionCommandType.AllProductData;
        private PositionCommandType currentPositionCommandTypeLine3 = PositionCommandType.AllProductData;

        private ProductInfoType productInfoTypeLine1 = ProductInfoType.AllProductInfo;
        private ProductInfoType productInfoTypeLine2 = ProductInfoType.AllProductInfo;
        private ProductInfoType productInfoTypeLine3 = ProductInfoType.AllProductInfo;

        // Position Management
        private int minPositionLine1 = 0;
        private int maxPositionLine1 = 0;
        private int currentStartPositionLine1 = 0;
        private int currentEndPositionLine1 = 0;

        private int minPositionLine2 = 0;
        private int maxPositionLine2 = 0;
        private int currentStartPositionLine2 = 0;
        private int currentEndPositionLine2 = 0;

        private int minPositionLine3 = 0;
        private int maxPositionLine3 = 0;
        private int currentStartPositionLine3 = 0;
        private int currentEndPositionLine3 = 0;

        private Dictionary<int, string> positionStatusLine1 = new Dictionary<int, string>();
        private Dictionary<int, string> positionStatusLine2 = new Dictionary<int, string>();
        private Dictionary<int, string> positionStatusLine3 = new Dictionary<int, string>();

        private bool hasSHAPositionLine1 = false;
        private bool hasHPositionLine1 = false;
        private bool hasSAPositionLine1 = false;

        private bool hasSHAPositionLine2 = false;
        private bool hasHPositionLine2 = false;
        private bool hasSAPositionLine2 = false;

        private bool hasSHAPositionLine3 = false;
        private bool hasHPositionLine3 = false;
        private bool hasSAPositionLine3 = false;

        private Dictionary<int, PositionStatusInfo> positionStatusInfoLine1 = new Dictionary<int, PositionStatusInfo>();
        private Dictionary<int, PositionStatusInfo> positionStatusInfoLine2 = new Dictionary<int, PositionStatusInfo>();
        private Dictionary<int, PositionStatusInfo> positionStatusInfoLine3 = new Dictionary<int, PositionStatusInfo>();

        private bool isSBGZInfoShowLine1 = false;
        private bool isPZBLInfoShowLine1 = false;
        private bool isBPQPInfoShowLine1 = false;
        private bool IsExcep1 = false;
        private bool IsStopPlan1 = false;

        private bool isSBGZInfoShowLine2 = false;
        private bool isPZBLInfoShowLine2 = false;
        private bool isBPQPInfoShowLine2 = false;
        private bool IsExcep2 = false;
        private bool IsStopPlan2 = false;

        private bool isSBGZInfoShowLine3 = false;
        private bool isPZBLInfoShowLine3 = false;
        private bool isBPQPInfoShowLine3 = false;
        private bool IsExcep3 = false;
        private bool IsStopPlan3 = false;

        /// <summary>
        /// Constructor
        /// </summary>
        public ProductManagementView()
        {
            InitializeComponent();

            this.timer.Enabled = false;

            string defaultValue = "0";

            this.lblDayPlanLine1.Text = defaultValue;
            this.lblCurrentPlanLine1.Text = defaultValue;
            this.lblActualCompletedLine1.Text = defaultValue;
            this.lblStopTimeLine1.Text = defaultValue;

            this.lblDayPlanLine2.Text = defaultValue;
            this.lblCurrentPlanLine2.Text = defaultValue;
            this.lblActualCompletedLine2.Text = defaultValue;
            this.lblStopTimeLine2.Text = defaultValue;

            this.lblDayPlanLine3.Text = defaultValue;
            this.lblCurrentPlanLine3.Text = defaultValue;
            this.lblActualCompletedLine3.Text = defaultValue;
            this.lblStopTimeLine3.Text = defaultValue;

            this.tlpCommentButtonLine1.Visible = ConfigInfo.IsShowCommentButton;
            this.tlpCommentButtonLine2.Visible = ConfigInfo.IsShowCommentButton;
            this.tlpCommentButtonLine3.Visible = ConfigInfo.IsShowCommentButton;

            //this.btnSBGZLine1.Visible = ConfigInfo.IsShowCommentButton;
            //this.btnPZBLLine1.Visible = ConfigInfo.IsShowCommentButton;
            //this.btnBPQPLine1.Visible = ConfigInfo.IsShowCommentButton;

            //this.btnSBGZLine2.Visible = ConfigInfo.IsShowCommentButton;
            //this.btnPZBLLine2.Visible = ConfigInfo.IsShowCommentButton;
            //this.btnBPQPLine2.Visible = ConfigInfo.IsShowCommentButton;

            //this.btnSBGZLine3.Visible = ConfigInfo.IsShowCommentButton;
            //this.btnPZBLLine3.Visible = ConfigInfo.IsShowCommentButton;
            //this.btnBPQPLine3.Visible = ConfigInfo.IsShowCommentButton;

            this.ClearPositionInfo(ConfigInfo.LineCode1);
            this.ClearPositionInfo(ConfigInfo.LineCode2);
            this.ClearPositionInfo(ConfigInfo.LineCode3);

            this.InitFont();
        }

        /// <summary>
        /// Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductManagementView_Load(object sender, EventArgs e)
        {
            this.GetUserAndLineCode();
            this.GetAllLines();
            this.InitOperationArea();
            this.InitDMPositionAndCommandType();

            this.minPositionLine1 = ConfigInfo.GetMinPosition(ConfigInfo.LineCode1);
            this.maxPositionLine1 = ConfigInfo.GetMaxPosition(ConfigInfo.LineCode1);
            this.currentStartPositionLine1 = this.minPositionLine1;
            this.currentEndPositionLine1 = this.minPositionLine1;

            this.minPositionLine2 = ConfigInfo.GetMinPosition(ConfigInfo.LineCode2);
            this.maxPositionLine2 = ConfigInfo.GetMaxPosition(ConfigInfo.LineCode2);
            this.currentStartPositionLine2 = this.minPositionLine2;
            this.currentEndPositionLine2 = this.minPositionLine2;

            this.minPositionLine3 = ConfigInfo.GetMinPosition(ConfigInfo.LineCode3);
            this.maxPositionLine3 = ConfigInfo.GetMaxPosition(ConfigInfo.LineCode3);
            this.currentStartPositionLine3 = this.minPositionLine3;
            this.currentEndPositionLine3 = this.minPositionLine3;
        }

        /// <summary>
        /// Shown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductManagementView_Shown(object sender, EventArgs e)
        {
            Util.PositionNormalTime = DateTime.Now;

            if (ConfigInfo.CurrentLineCodeCount >= 1)
            {
                // Send Line1 Data
                if (productInfoTypeLine1 == ProductInfoType.AllProductInfo)
                {
                    this.currentPositionCommandTypeLine1 = PositionCommandType.AllProductData;
                    this.serialPortOperator1.WriteLine(COMMAND_HEADER + planNumPositionLine1 + "0004");
                }
                else
                {
                    this.currentPositionCommandTypeLine1 = PositionCommandType.PlanNum;
                    this.serialPortOperator1.WriteLine(COMMAND_HEADER + planNumPositionLine1 + "0001");
                }
            }

            if (ConfigInfo.CurrentLineCodeCount >= 2)
            {
                // Send Line2 Data
                if (productInfoTypeLine2 == ProductInfoType.AllProductInfo)
                {
                    this.currentPositionCommandTypeLine2 = PositionCommandType.AllProductData;
                    this.serialPortOperator2.WriteLine(COMMAND_HEADER + planNumPositionLine2 + "0004");
                }
                else
                {
                    this.currentPositionCommandTypeLine2 = PositionCommandType.PlanNum;
                    this.serialPortOperator2.WriteLine(COMMAND_HEADER + planNumPositionLine2 + "0001");
                }
            }

            if (ConfigInfo.CurrentLineCodeCount >= 3)
            {
                // Send Line3 Data
                if (productInfoTypeLine3 == ProductInfoType.AllProductInfo)
                {
                    this.currentPositionCommandTypeLine3 = PositionCommandType.AllProductData;
                    this.serialPortOperator3.WriteLine(COMMAND_HEADER + planNumPositionLine3 + "0004");
                }
                else
                {
                    this.currentPositionCommandTypeLine3 = PositionCommandType.PlanNum;
                    this.serialPortOperator3.WriteLine(COMMAND_HEADER + planNumPositionLine3 + "0001");
                }
            }

            this.timer.Enabled = true;
        }

        /// <summary>
        /// Get User And Line Code
        /// </summary>
        private void GetUserAndLineCode()
        {
            this.userAndLineCodeModel = Account.GetUserAndLineCode(LoginInfo.UserName);
            ConfigInfo.CurrentLineCodeCount = this.userAndLineCodeModel.UserAndLineCodeTable.Rows.Count;
        }

        /// <summary>
        /// Get All Lines
        /// </summary>
        private void GetAllLines()
        {
            ConfigInfo.DtAllLines = Product.GetAllLines();
        }

        /// <summary>
        /// Init Operation Area
        /// </summary>
        private void InitOperationArea()
        {
            this.tlpPositionAndCommentInfoLine1.ColumnStyles.Clear();
            this.tlpPositionAndCommentInfoLine1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpPositionAndCommentInfoLine1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));

            this.tlpPositionAndCommentInfoLine2.ColumnStyles.Clear();
            this.tlpPositionAndCommentInfoLine2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpPositionAndCommentInfoLine2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));

            this.tlpPositionAndCommentInfoLine3.ColumnStyles.Clear();
            this.tlpPositionAndCommentInfoLine3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpPositionAndCommentInfoLine3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));

            if (ConfigInfo.CurrentLineCodeCount <= 0)
            {
                this.tlpMain.Visible = false;
            }

            if (ConfigInfo.CurrentLineCodeCount >= 3)
            {
                ConfigInfo.LineCode3
                    = this.userAndLineCodeModel.UserAndLineCodeTable.Rows[2]["Flow"].ToString().Trim();

                string lineName3 = ConfigInfo.GetLineNameByCode(ConfigInfo.LineCode3);

                ConfigInfo.DtLineWatchSys3 = Config.GetLineWatchSys(ConfigInfo.LineCode3);

                //this.lblShiftLine3.Text = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys3);

                this.lblProductManagement3.Text = lineName3;

                ////this.tlpOperationArea1.Visible = true;
                ////this.tlpOperationArea2.Visible = true;
                ////this.tlpOperationArea3.Visible = true;

                ////this.tlpMain.ColumnStyles.Clear();
                ////this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                ////this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                ////this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                ////this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                ////this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                ////this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                ////this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));

                string serialPortName3 = ConfigInfo.GetLineSerialPortByCode(ConfigInfo.LineCode3);
                this.serialPortsAndLineCodes.Add(serialPortName3, ConfigInfo.LineCode3);

                try
                {
                    this.serialPortOperator3 = new SerialPortOperator();
                    this.Controls.Add(this.serialPortOperator3);
                    this.serialPortOperator3.InitPort(serialPortName3, 9600);
                    this.serialPortOperator3.InterfaceUpdateHandle
                               = new InterfaceUpdateHandler(InterfaceUpdate3);

                    this.lineCodesAndSerialPortOperators.Add(ConfigInfo.LineCode3, this.serialPortOperator3);
                }
                catch
                {

                }
            }

            if (ConfigInfo.CurrentLineCodeCount >= 2)
            {
                ConfigInfo.LineCode2
                    = this.userAndLineCodeModel.UserAndLineCodeTable.Rows[1]["Flow"].ToString().Trim();

                string lineName2 = ConfigInfo.GetLineNameByCode(ConfigInfo.LineCode2);

                ConfigInfo.DtLineWatchSys2 = Config.GetLineWatchSys(ConfigInfo.LineCode2);

                //this.lblShiftLine2.Text = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys2);

                this.lblProductManagement2.Text = lineName2;

                if (ConfigInfo.CurrentLineCodeCount == 2)
                {
                    this.tlpOperationArea1.Visible = true;
                    this.tlpOperationArea2.Visible = true;
                    this.tlpOperationArea3.Visible = false;
                    this.panel2.Visible = false;

                    this.tlpMain.ColumnStyles.Clear();
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 3F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
                }

                string serialPortName2 = ConfigInfo.GetLineSerialPortByCode(ConfigInfo.LineCode2);
                this.serialPortsAndLineCodes.Add(serialPortName2, ConfigInfo.LineCode2);

                try
                {
                    this.serialPortOperator2 = new SerialPortOperator();
                    this.Controls.Add(this.serialPortOperator2);
                    this.serialPortOperator2.InitPort(serialPortName2, 9600);
                    this.serialPortOperator2.InterfaceUpdateHandle
                               = new InterfaceUpdateHandler(InterfaceUpdate2);

                    this.lineCodesAndSerialPortOperators.Add(ConfigInfo.LineCode2, this.serialPortOperator2);
                }
                catch
                {

                }
            }

            if (ConfigInfo.CurrentLineCodeCount >= 1)
            {
                ConfigInfo.LineCode1
                    = this.userAndLineCodeModel.UserAndLineCodeTable.Rows[0]["Flow"].ToString().Trim();

                string lineName1 = ConfigInfo.GetLineNameByCode(ConfigInfo.LineCode1);

                ConfigInfo.DtLineWatchSys1 = Config.GetLineWatchSys(ConfigInfo.LineCode1);

                this.lblShiftLine1.Text = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys1);

                this.lblProductManagement1.Text = lineName1;

                if (ConfigInfo.CurrentLineCodeCount == 1)
                {
                    this.tlpOperationArea1.Visible = true;
                    this.tlpOperationArea2.Visible = false;
                    this.tlpOperationArea3.Visible = false;
                    this.panel1.Visible = false;
                    this.panel2.Visible = false;

                    this.tlpMain.ColumnStyles.Clear();
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 30F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 30F));
                }

                string serialPortName1 = ConfigInfo.GetLineSerialPortByCode(ConfigInfo.LineCode1);
                this.serialPortsAndLineCodes.Add(serialPortName1, ConfigInfo.LineCode1);

                try
                {
                    this.serialPortOperator1 = new SerialPortOperator();
                    this.Controls.Add(this.serialPortOperator1);
                    this.serialPortOperator1.InitPort(serialPortName1, 9600);
                    this.serialPortOperator1.InterfaceUpdateHandle
                               = new InterfaceUpdateHandler(InterfaceUpdate1);

                    this.lineCodesAndSerialPortOperators.Add(ConfigInfo.LineCode1, this.serialPortOperator1);
                }
                catch
                {

                }
            }
        }

        /// <summary>
        /// Init Font
        /// </summary>
        private void InitFont()
        {
            foreach (Control control in this.Controls)
            {
                if (control.GetType() == typeof(Label))
                {
                    Label lbl = (Label)control;
                    if (lbl.Tag != null)
                    {
                        this.UpdateFont(lbl);
                    }
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Init Font
        /// </summary>
        /// <param name="controlParent"></param>
        private void InitFont(Control controlParent)
        {
            foreach (Control control in controlParent.Controls)
            {
                if (control.GetType() == typeof(Label))
                {
                    Label lbl = (Label)control;
                    if (lbl.Tag != null)
                    {
                        this.UpdateFont(lbl);
                    }
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Update Font
        /// </summary>
        /// <param name="control"></param>
        private void UpdateFont(Control control)
        {
            if (control.Tag.ToString().StartsWith("FontSizeTitle"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.ProductFontSizeTitle, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeCaption"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.ProductFontSizeCaption, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeValue"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.ProductFontSizeValue, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("PositionFontSizeValue"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.PositionFontSizeValue, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("CommentFontSizeTitle"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.CommentFontSizeTitle, FontStyle.Bold);
            }
        }

        /// <summary>
        /// Set Font
        /// </summary>
        /// <param name="control"></param>
        /// <param name="familyName"></param>
        /// <param name="emSize"></param>
        /// <param name="style"></param>
        private void SetFont(Control control, string familyName, float emSize, FontStyle style)
        {
            control.Font = new System.Drawing.Font(familyName,
                                                   emSize,
                                                   style,
                                                   System.Drawing.GraphicsUnit.Point,
                                                   ((byte)(0))
                                                   );
        }

        /// <summary>
        /// Init DM Position And Command Type
        /// </summary>
        private void InitDMPositionAndCommandType()
        {
            this.planNumPositionLine1 = ConfigurationManager.AppSettings["PlanNumDMPositionLine1"].ToString().Trim();
            this.nowPlanPositionLine1 = ConfigurationManager.AppSettings["NowPlanDMPositionLine1"].ToString().Trim();
            this.productNumPositionLine1 = ConfigurationManager.AppSettings["ProductNumDMPositionLine1"].ToString().Trim();
            this.stopTimePositionLine1 = ConfigurationManager.AppSettings["StopTimeDMPositionLine1"].ToString().Trim();
            this.ExcepDMPositionLine1 = ConfigurationManager.AppSettings["ExcepDMPositionLine1"].ToString().Trim();
            this.StopPlanDMPositionLine1 = ConfigurationManager.AppSettings["StopPlanDMPositionLine1"].ToString().Trim();

            this.planNumPositionLine2 = ConfigurationManager.AppSettings["PlanNumDMPositionLine2"].ToString().Trim();
            this.nowPlanPositionLine2 = ConfigurationManager.AppSettings["NowPlanDMPositionLine2"].ToString().Trim();
            this.productNumPositionLine2 = ConfigurationManager.AppSettings["ProductNumDMPositionLine2"].ToString().Trim();
            this.stopTimePositionLine2 = ConfigurationManager.AppSettings["StopTimeDMPositionLine2"].ToString().Trim();
            this.ExcepDMPositionLine2 = ConfigurationManager.AppSettings["ExcepDMPositionLine2"].ToString().Trim();
            this.StopPlanDMPositionLine2 = ConfigurationManager.AppSettings["StopPlanDMPositionLine2"].ToString().Trim();

            this.planNumPositionLine3 = ConfigurationManager.AppSettings["PlanNumDMPositionLine3"].ToString().Trim();
            this.nowPlanPositionLine3 = ConfigurationManager.AppSettings["NowPlanDMPositionLine3"].ToString().Trim();
            this.productNumPositionLine3 = ConfigurationManager.AppSettings["ProductNumDMPositionLine3"].ToString().Trim();
            this.stopTimePositionLine3 = ConfigurationManager.AppSettings["StopTimeDMPositionLine3"].ToString().Trim();
            this.ExcepDMPositionLine3 = ConfigurationManager.AppSettings["ExcepDMPositionLine3"].ToString().Trim();
            this.StopPlanDMPositionLine3 = ConfigurationManager.AppSettings["StopPlanDMPositionLine3"].ToString().Trim();

            // Init Line1 Status
            int planNumPositionTemp = int.Parse(this.planNumPositionLine1);
            int nowPlanPositionTemp = int.Parse(this.nowPlanPositionLine1);
            int productNumPositionTemp = int.Parse(this.productNumPositionLine1);
            int stopTimePositionTemp = int.Parse(this.stopTimePositionLine1);

            if ((planNumPositionTemp + 1 == nowPlanPositionTemp)
                && (planNumPositionTemp + 2 ==stopTimePositionTemp)
                && (planNumPositionTemp + 3 ==productNumPositionTemp))
            {
                this.currentPositionCommandTypeLine1 = PositionCommandType.AllProductData;
                this.productInfoTypeLine1 = ProductInfoType.AllProductInfo;
            }
            else
            {
                this.currentPositionCommandTypeLine1 = PositionCommandType.PlanNum;
                this.productInfoTypeLine1 = ProductInfoType.SingleProdutInfo;
            }

            // Init Line2 Status
            planNumPositionTemp = int.Parse(this.planNumPositionLine2);
            nowPlanPositionTemp = int.Parse(this.nowPlanPositionLine2);
            productNumPositionTemp = int.Parse(this.productNumPositionLine2);
            stopTimePositionTemp = int.Parse(this.stopTimePositionLine2);

            if ((planNumPositionTemp + 1 == nowPlanPositionTemp)
                && (planNumPositionTemp + 2 == stopTimePositionTemp)
                && (planNumPositionTemp + 3 == productNumPositionTemp))
            {
                this.currentPositionCommandTypeLine2 = PositionCommandType.AllProductData;
                this.productInfoTypeLine2 = ProductInfoType.AllProductInfo;
            }
            else
            {
                this.currentPositionCommandTypeLine2 = PositionCommandType.PlanNum;
                this.productInfoTypeLine2 = ProductInfoType.SingleProdutInfo;
            }

            // Init Line3 Status
            planNumPositionTemp = int.Parse(this.planNumPositionLine3);
            nowPlanPositionTemp = int.Parse(this.nowPlanPositionLine3);
            productNumPositionTemp = int.Parse(this.productNumPositionLine3);
            stopTimePositionTemp = int.Parse(this.stopTimePositionLine3);

            if ((planNumPositionTemp + 1 == nowPlanPositionTemp)
                && (planNumPositionTemp + 2 == stopTimePositionTemp)
                && (planNumPositionTemp + 3 == productNumPositionTemp))
            {
                this.currentPositionCommandTypeLine3 = PositionCommandType.AllProductData;
                this.productInfoTypeLine3 = ProductInfoType.AllProductInfo;
            }
            else
            {
                this.currentPositionCommandTypeLine3 = PositionCommandType.PlanNum;
                this.productInfoTypeLine3 = ProductInfoType.SingleProdutInfo;
            }
        }

        /// <summary>
        /// Interface Update 1
        /// </summary>
        /// <param name="text"></param>
        /// <param name="portName"></param>
        private void InterfaceUpdate1(string text, string portName)
        {
            this.ResolveDataAndSendCommand(text, portName,
                                           ref this.currentPositionCommandTypeLine1,
                                           this.productInfoTypeLine1,
                                           this.serialPortOperator1,
                                           this.planNumPositionLine1,
                                           this.nowPlanPositionLine1,
                                           this.productNumPositionLine1,
                                           this.stopTimePositionLine1,
                                           this.ExcepDMPositionLine1,
                                           this.StopPlanDMPositionLine1,
                                           ConfigInfo.AlarmDMPositionLine1,
                                           ConfigInfo.HelpDMPositionLine1,
                                           this.hasSAPositionLine1,
                                           this.hasHPositionLine1,
                                           this.minPositionLine1,
                                           this.maxPositionLine1,
                                           ref ConfigInfo.PlanNumSettingCommandLine1,
                                           ref ConfigInfo.NowPlanSettingCommandLine1,
                                           ref ConfigInfo.PlanNumResetCommandLine1,
                                           ref ConfigInfo.NowPlanResetCommandLine1,
                                           ref ConfigInfo.ProductNumResetCommandLine1,
                                           ref ConfigInfo.StopTimeResetCommandLine1,
                                           ref this.currentStartPositionLine1,
                                           ref this.currentEndPositionLine1,
                                           this.positionStatusLine1
                                           );
        }

        /// <summary>
        /// Interface Update 2
        /// </summary>
        /// <param name="text"></param>
        /// <param name="portName"></param>
        private void InterfaceUpdate2(string text, string portName)
        {
            this.ResolveDataAndSendCommand(text, portName,
                                           ref this.currentPositionCommandTypeLine2,
                                           this.productInfoTypeLine2,
                                           this.serialPortOperator2,
                                           this.planNumPositionLine2,
                                           this.nowPlanPositionLine2,
                                           this.productNumPositionLine2,
                                           this.stopTimePositionLine2,
                                           this.ExcepDMPositionLine2,
                                           this.StopPlanDMPositionLine2,
                                           ConfigInfo.AlarmDMPositionLine2,
                                           ConfigInfo.HelpDMPositionLine2,
                                           this.hasSAPositionLine2,
                                           this.hasHPositionLine2,
                                           this.minPositionLine2,
                                           this.maxPositionLine2,
                                           ref ConfigInfo.PlanNumSettingCommandLine2,
                                           ref ConfigInfo.NowPlanSettingCommandLine2,
                                           ref ConfigInfo.PlanNumResetCommandLine2,
                                           ref ConfigInfo.NowPlanResetCommandLine2,
                                           ref ConfigInfo.ProductNumResetCommandLine2,
                                           ref ConfigInfo.StopTimeResetCommandLine2,
                                           ref this.currentStartPositionLine2,
                                           ref this.currentEndPositionLine2,
                                           this.positionStatusLine2
                                           );
        }

        /// <summary>
        /// Interface Update 3
        /// </summary>
        /// <param name="text"></param>
        /// <param name="portName"></param>
        private void InterfaceUpdate3(string text, string portName)
        {
            this.ResolveDataAndSendCommand(text, portName,
                                           ref this.currentPositionCommandTypeLine3,
                                           this.productInfoTypeLine3,
                                           this.serialPortOperator3,
                                           this.planNumPositionLine3,
                                           this.nowPlanPositionLine3,
                                           this.productNumPositionLine3,
                                           this.stopTimePositionLine3,
                                           this.ExcepDMPositionLine2,
                                           this.StopPlanDMPositionLine2,
                                           ConfigInfo.AlarmDMPositionLine3,
                                           ConfigInfo.HelpDMPositionLine3,
                                           this.hasSAPositionLine3,
                                           this.hasHPositionLine3,
                                           this.minPositionLine3,
                                           this.maxPositionLine3,
                                           ref ConfigInfo.PlanNumSettingCommandLine3,
                                           ref ConfigInfo.NowPlanSettingCommandLine3,
                                           ref ConfigInfo.PlanNumResetCommandLine3,
                                           ref ConfigInfo.NowPlanResetCommandLine3,
                                           ref ConfigInfo.ProductNumResetCommandLine3,
                                           ref ConfigInfo.StopTimeResetCommandLine3,
                                           ref this.currentStartPositionLine3,
                                           ref this.currentEndPositionLine3,
                                           this.positionStatusLine3
                                           );
        }

        /// <summary>
        /// Resolve Data And Send Command
        /// </summary>
        /// <param name="text"></param>
        /// <param name="portName"></param>
        /// <param name="currentPositionCommandType"></param>
        /// <param name="productInfoType"></param>
        /// <param name="serialPortOperator"></param>
        /// <param name="planNumPosition"></param>
        /// <param name="productNumPosition"></param>
        /// <param name="stopTimePosition"></param>
        /// <param name="maxPosition"></param>
        /// <param name="currentStartPosition"></param>
        /// <param name="currentEndPosition"></param>
        /// <param name="positionStatus"></param>
        private void ResolveDataAndSendCommand(string text, string portName,
                                               ref PositionCommandType currentPositionCommandType,
                                               ProductInfoType productInfoType,
                                               SerialPortOperator serialPortOperator,
                                               string planNumPosition,
                                               string nowPlanPosition,
                                               string productNumPosition,
                                               string stopTimePosition,
                                               string Exceposition,
                                               string StopPlanosition,
                                               string alarmDMPosition,
                                               string helpDMPosition,
                                               bool hasSAPosition,
                                               bool hasHPosition,
                                               int minPosition,
                                               int maxPosition,
                                               ref string planNumSettingCommand,
                                               ref string nowPlanSettingCommand,
                                               ref string planNumResetCommand,
                                               ref string nowPlanResetCommand,
                                               ref string productNumResetCommand,
                                               ref string stopTimeResetCommand,
                                               ref int currentStartPosition,
                                               ref int currentEndPosition,
                                               Dictionary<int, string> positionStatus)
        {
            if (currentPositionCommandType == PositionCommandType.AllProductData)
            {
                int planNumGroupIndex = 1;
                int nowPlanGroupIndex = 2;
                int productNumGroupIndex = 3;
                int stopTimeGroupIndex = 4;
                int ExcepGroupIndex = 5;
                int StopPlanGroupIndex = 6;

                string planNum = this.GetProductDataByGroup(text, planNumGroupIndex);
                string nowPlan = this.GetProductDataByGroup(text, nowPlanGroupIndex);
                string productNum = this.GetProductDataByGroup(text, productNumGroupIndex);
                string stopTime = this.GetProductDataByGroup(text, stopTimeGroupIndex);
                string Excep = this.GetProductDataByGroup(text, ExcepGroupIndex);
                string StopPlan = this.GetProductDataByGroup(text, StopPlanGroupIndex);

                this.UpdateUI(planNum, nowPlan, productNum, stopTime, portName);
                this.Update_Exception(Excep, portName);
                this.Update_StopPlan(StopPlan, portName);

                currentPositionCommandType = PositionCommandType.PositionSHA;
                serialPortOperator.WriteLine(COMMAND_HEADER
                                             + currentStartPosition.ToString("0000")
                                             + ConfigInfo.PositionNumInOneCommand.ToString("0000"));

                currentEndPosition = currentStartPosition + ConfigInfo.PositionNumInOneCommand - 1;
            }
            else if (currentPositionCommandType == PositionCommandType.PlanNum)
            {
                int planNumGroupIndex = 1;

                string planNum = this.GetProductDataByGroup(text, planNumGroupIndex);
                string nowPlan = string.Empty;
                string productNum = string.Empty;
                string stopTime = string.Empty;
                string Excep = string.Empty;
                string StopPlan = string.Empty;

                this.UpdateUI(planNum, nowPlan, productNum, stopTime, portName);

                currentPositionCommandType = PositionCommandType.NowPlan;
                serialPortOperator.WriteLine(COMMAND_HEADER + nowPlanPosition + "0001");
            }
            else if (currentPositionCommandType == PositionCommandType.NowPlan)
            {
                int groupIndex = 1;

                string planNum = string.Empty;
                string nowPlan = this.GetProductDataByGroup(text, groupIndex);
                string productNum = string.Empty;
                string stopTime = string.Empty;
                string Excep = string.Empty;
                string StopPlan = string.Empty;

                this.UpdateUI(planNum, nowPlan, productNum, stopTime, portName);

                currentPositionCommandType = PositionCommandType.ProductNum;
                serialPortOperator.WriteLine(COMMAND_HEADER + productNumPosition + "0001");
            }
            else if (currentPositionCommandType == PositionCommandType.ProductNum)
            {
                int productNumGroupIndex = 1;

                string planNum = string.Empty;
                string nowPlan = string.Empty;
                string productNum = this.GetProductDataByGroup(text, productNumGroupIndex);
                string stopTime = string.Empty;
                string Excep = string.Empty;
                string StopPlan = string.Empty;

                this.UpdateUI(planNum, nowPlan, productNum, stopTime, portName);

                currentPositionCommandType = PositionCommandType.Excep;
                serialPortOperator.WriteLine(COMMAND_HEADER + Exceposition + "0001");
            }
            else if (currentPositionCommandType == PositionCommandType.Excep)
            {
                int ExcepGroupIndex = 1;

                string planNum = string.Empty;
                string nowPlan = string.Empty;
                string productNum = string.Empty;
                string stopTime = string.Empty;
                string Excep = this.GetProductDataByGroup(text, ExcepGroupIndex);
                string StopPlan = string.Empty;

                this.Update_Exception(Excep,portName);

                currentPositionCommandType = PositionCommandType.StopPlan;
                serialPortOperator.WriteLine(COMMAND_HEADER + StopPlanosition + "0001");
            }
            else if (currentPositionCommandType == PositionCommandType.StopPlan)
            {
                int StopPlanGroupIndex = 1;

                string planNum = string.Empty;
                string nowPlan = string.Empty;
                string productNum = string.Empty;
                string stopTime = string.Empty;
                string Excep = string.Empty;
                string StopPlan = this.GetProductDataByGroup(text, StopPlanGroupIndex);

                this.Update_StopPlan(StopPlan, portName);

                currentPositionCommandType = PositionCommandType.StopTime;
                serialPortOperator.WriteLine(COMMAND_HEADER + stopTimePosition + "0001");
            }
            else if (currentPositionCommandType == PositionCommandType.StopTime)
            {
                int stopTimeGroupIndex = 1;

                string planNum = string.Empty;
                string nowPlan = string.Empty;
                string productNum = string.Empty;
                string stopTime = this.GetProductDataByGroup(text, stopTimeGroupIndex);
                string Excep = string.Empty;
                string StopPlan = string.Empty;

                this.UpdateUI(planNum, nowPlan, productNum, stopTime, portName);

                //////currentPositionCommandType = PositionCommandType.PositionSHA;

                //////serialPortOperator.WriteLine(COMMAND_HEADER
                //////                             + currentStartPosition.ToString("0000")
                //////                             + ConfigInfo.PositionNumInOneCommand.ToString("0000"));

                //////currentEndPosition = currentStartPosition + ConfigInfo.PositionNumInOneCommand - 1;

                currentPositionCommandType = PositionCommandType.PlanNumSetting;

                if (!string.IsNullOrWhiteSpace(planNumSettingCommand))
                {
                    serialPortOperator.WriteLine(planNumSettingCommand);
                    planNumSettingCommand = string.Empty;
                }
                else
                {
                    serialPortOperator.WriteLine(COMMAND_HEADER + planNumPosition + "0001");
                }
            }
            else if (currentPositionCommandType == PositionCommandType.PlanNumSetting)
            {
                currentPositionCommandType = PositionCommandType.NowPlanSetting;

                if (!string.IsNullOrWhiteSpace(nowPlanSettingCommand))
                {
                    serialPortOperator.WriteLine(nowPlanSettingCommand);
                    nowPlanSettingCommand = string.Empty;
                }
                else
                {
                    serialPortOperator.WriteLine(COMMAND_HEADER + planNumPosition + "0001");
                }
            }
            else if (currentPositionCommandType == PositionCommandType.NowPlanSetting)
            {
                currentPositionCommandType = PositionCommandType.PlanNumReset;

                if (!string.IsNullOrWhiteSpace(planNumResetCommand))
                {
                    serialPortOperator.WriteLine(planNumResetCommand);
                    planNumResetCommand = string.Empty;
                }
                else
                {
                    serialPortOperator.WriteLine(COMMAND_HEADER + planNumPosition + "0001");
                }
            }
            else if (currentPositionCommandType == PositionCommandType.PlanNumReset)
            {
                currentPositionCommandType = PositionCommandType.NowPlanReset;

                if (!string.IsNullOrWhiteSpace(nowPlanResetCommand))
                {
                    serialPortOperator.WriteLine(nowPlanResetCommand);
                    nowPlanResetCommand = string.Empty;
                }
                else
                {
                    serialPortOperator.WriteLine(COMMAND_HEADER + planNumPosition + "0001");
                }
            }
            else if (currentPositionCommandType == PositionCommandType.NowPlanReset)
            {
                currentPositionCommandType = PositionCommandType.ProductNumReset;

                if (!string.IsNullOrWhiteSpace(productNumResetCommand))
                {
                    serialPortOperator.WriteLine(productNumResetCommand);
                    productNumResetCommand = string.Empty;
                }
                else
                {
                    serialPortOperator.WriteLine(COMMAND_HEADER + planNumPosition + "0001");
                }
            }
            else if (currentPositionCommandType == PositionCommandType.ProductNumReset)
            {
                currentPositionCommandType = PositionCommandType.StopTimeReset;

                if (!string.IsNullOrWhiteSpace(stopTimeResetCommand))
                {
                    serialPortOperator.WriteLine(stopTimeResetCommand);
                    stopTimeResetCommand = string.Empty;
                }
                else
                {
                    serialPortOperator.WriteLine(COMMAND_HEADER + planNumPosition + "0001");
                }
            }
            else if (currentPositionCommandType == PositionCommandType.StopTimeReset)
            {
                currentPositionCommandType = PositionCommandType.PositionSHA;

                serialPortOperator.WriteLine(COMMAND_HEADER
                                             + currentStartPosition.ToString("0000")
                                             + ConfigInfo.PositionNumInOneCommand.ToString("0000"));

                currentEndPosition = currentStartPosition + ConfigInfo.PositionNumInOneCommand - 1;
            }
            else if (currentPositionCommandType == PositionCommandType.PositionSHA)
            {
                this.GetPositionStatus(text, currentStartPosition, currentEndPosition, positionStatus);

                // Update Position Management Form
                this.UpdatePositionUI(positionStatus, portName);

                if (currentEndPosition >= maxPosition)
                {
                    currentEndPosition = minPosition;
                    currentStartPosition = minPosition;

                    currentPositionCommandType = PositionCommandType.PositionMusicA;

                    if (hasSAPosition)
                    {
                        serialPortOperator.WriteLine(WRITE_COMMAND_HEADER + alarmDMPosition + "0001");
                    }
                    else
                    {
                        serialPortOperator.WriteLine(WRITE_COMMAND_HEADER + alarmDMPosition + "0000");
                    }

                    //////if (productInfoType == ProductInfoType.AllProductInfo)
                    //////{
                    //////    currentPositionCommandType = PositionCommandType.AllProductData;
                    //////    serialPortOperator.WriteLine(COMMAND_HEADER + planNumPosition + "0004");
                    //////}
                    //////else
                    //////{
                    //////    currentPositionCommandType = PositionCommandType.PlanNum;
                    //////    serialPortOperator.WriteLine(COMMAND_HEADER + planNumPosition + "0001");
                    //////}
                }
                else
                {
                    currentStartPosition = currentEndPosition + 1;
                    currentPositionCommandType = PositionCommandType.PositionSHA;
                    serialPortOperator.WriteLine(COMMAND_HEADER
                                                 + currentStartPosition.ToString("0000")
                                                 + ConfigInfo.PositionNumInOneCommand.ToString("0000"));

                    currentEndPosition = currentStartPosition + ConfigInfo.PositionNumInOneCommand - 1;
                }
            }
            else if (currentPositionCommandType == PositionCommandType.PositionMusicA)
            {
                currentPositionCommandType = PositionCommandType.PositionMusicH;

                if (hasHPosition)
                {
                    serialPortOperator.WriteLine(WRITE_COMMAND_HEADER + helpDMPosition + "0001");
                }
                else
                {
                    serialPortOperator.WriteLine(WRITE_COMMAND_HEADER + helpDMPosition + "0000");
                }
            }
            else if (currentPositionCommandType == PositionCommandType.PositionMusicH)
            {
                if (productInfoType == ProductInfoType.AllProductInfo)
                {
                    currentPositionCommandType = PositionCommandType.AllProductData;
                    serialPortOperator.WriteLine(COMMAND_HEADER + planNumPosition + "0004");
                }
                else
                {
                    currentPositionCommandType = PositionCommandType.PlanNum;
                    serialPortOperator.WriteLine(COMMAND_HEADER + planNumPosition + "0001");
                }
            }
        }

        /// <summary>
        /// Update UI
        /// </summary>
        /// <param name="text"></param>
        /// <param name="portName"></param>
        private void UpdateUI(string planNum, string nowPlan, string productNum, string stopTime, string portName)
        {
            long planNumber = -1;
            long nowPlanNumber = -1;
            long productNumber = -1;
            long stopTimeNumber = -1;
            long temp = 0;

            if (long.TryParse(planNum, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out temp))
            {
                long.TryParse(planNum, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out planNumber);
            }

            if (long.TryParse(nowPlan, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out temp))
            {
                long.TryParse(nowPlan, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out nowPlanNumber);
            }

            if (long.TryParse(productNum, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out temp))
            {
                long.TryParse(productNum, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out productNumber);
            }

            if (long.TryParse(stopTime, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out temp))
            {
                long.TryParse(stopTime, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out stopTimeNumber);
            }

            string lineCode = this.serialPortsAndLineCodes[portName];
            this.UpdateProductInfo(planNumber, nowPlanNumber, productNumber, (stopTimeNumber == -1 ? -1 : (stopTimeNumber / 60)), lineCode);
        }

        /// <summary>
        /// Update Product Info
        /// </summary>
        /// <param name="lineCode"></param>
        /// <param name="planNum"></param>
        /// <param name="productNum"></param>
        /// <param name="stopTime"></param>
        private void UpdateProductInfo(long planNum, long nowPlan, long productNum, long stopTime, string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if (planNum >= 0)
                {
                    this.lblDayPlanLine1.Text = planNum.ToString();
                }

                if (nowPlan >= 0)
                {
                    this.lblCurrentPlanLine1.Text = nowPlan.ToString();
                }

                if (productNum >= 0)
                {
                    this.lblActualCompletedLine1.Text = productNum.ToString();
                }

                if (stopTime >= 0)
                {
                    this.lblStopTimeLine1.Text = stopTime.ToString();
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if (planNum >= 0)
                {
                    this.lblDayPlanLine2.Text = planNum.ToString();
                }

                if (nowPlan >= 0)
                {
                    this.lblCurrentPlanLine2.Text = nowPlan.ToString();
                }

                if (productNum >= 0)
                {
                    this.lblActualCompletedLine2.Text = productNum.ToString();
                }

                if (stopTime >= 0)
                {
                    this.lblStopTimeLine2.Text = stopTime.ToString();
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if (planNum >= 0)
                {
                    this.lblDayPlanLine3.Text = planNum.ToString();
                }

                if (nowPlan >= 0)
                {
                    this.lblCurrentPlanLine3.Text = nowPlan.ToString();
                }

                if (productNum >= 0)
                {
                    this.lblActualCompletedLine3.Text = productNum.ToString();
                }

                if (stopTime >= 0)
                {
                    this.lblStopTimeLine3.Text = stopTime.ToString();
                }
            }

            this.Refresh();
            this.Update();
        }

        /// <summary>
        /// Get Product Data By Group
        /// </summary>
        /// <param name="text"></param>
        /// <param name="groupIndex"></param>
        /// <returns></returns>
        private string GetProductDataByGroup(string text, int groupIndex)
        {
            if (groupIndex == 1)
            {
                return text.Substring(0, 4);
            }

            if (groupIndex == 2)
            {
                return text.Substring(4, 4);
            }

            if (groupIndex == 3)
            {
                return text.Substring(8, 4);
            }

            if (groupIndex == 4)
            {
                return text.Substring(12, 4);
            }

            if (groupIndex == 5)
            {
                return text.Substring(16, 4);
            }

            if (groupIndex == 6)
            {
                return text.Substring(20, 4);
            }

            return string.Empty;
        }

        /// <summary>
        /// Get Position Status
        /// </summary>
        /// <param name="text"></param>
        /// <param name="currentStartPosition"></param>
        /// <param name="currentEndPosition"></param>
        /// <param name="positionStatus"></param>
        private void GetPositionStatus(string status,
                                       int currentStartPosition,
                                       int currentEndPosition,
                                       Dictionary<int, string> positionStatus)
        {
            for (int position = currentStartPosition; position <= currentEndPosition; position++)
            {
                if (status.Length >= 4)
                {
                    if (!positionStatus.ContainsKey(position))
                    {
                        positionStatus.Add(position, status.Substring(0, 4));
                    }
                    else
                    {
                        positionStatus[position] = status.Substring(0, 4);
                    }

                    if (status.Length > 4)
                    {
                        status = status.Substring(4, status.Length - 4);
                    }
                    else
                    {
                        status = string.Empty;
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// Update Position UI
        /// </summary>
        /// <param name="positionStatus"></param>
        /// <param name="portName"></param>
        private void UpdatePositionUI(Dictionary<int, string> positionStatus, string portName)
        {
            string lineCode = this.serialPortsAndLineCodes[portName];
            this.UpdatePositionInfo(positionStatus, lineCode);

            if (this.HasSHAPosition())
            {
                if (this.screenSaver != null)
                {
                    this.screenSaver.Close();
                }

                Util.PositionNormalTime = DateTime.MaxValue;
            }
            else
            {
                if (Util.PositionNormalTime == DateTime.MaxValue)
                {
                    Util.PositionNormalTime = DateTime.Now;
                }
            }
        }

        /// <summary>
        /// Update Position Info
        /// </summary>
        /// <param name="positionStatus"></param>
        /// <param name="lineCode"></param>
        public void UpdatePositionInfo(Dictionary<int, string> positionStatus, string lineCode)
        {
            this.ClearPositionInfo(lineCode);

            foreach (int position in positionStatus.Keys)
            {
                DataRow row = this.GetPositionDataRow(position, lineCode);
                PositionType positionType = this.GetPositionType(row);

                string positionDesc = this.GetPositionDesc(row);
                string sPositionID = this.GetSPositionID(row);
                string fPositionID = this.GetFPositionID(row);
                PositionStatus pStatus = PositionStatus.Normal;

                if (!string.Equals("0000", positionStatus[position].ToString().Trim()))
                {
                    pStatus = PositionStatus.Unnormal;

                    if (!string.IsNullOrWhiteSpace(positionDesc))
                    {
                        if ((positionType == PositionType.Help)
                            || (positionType == PositionType.Unknown))
                        {
                            this.UpdateHelpPositionInfo(positionDesc, lineCode);
                        }
                        else if (positionType == PositionType.Alarm)
                        {
                            this.UpdateAlarmPositionInfo(positionDesc, lineCode);
                        }
                        else if (positionType == PositionType.Stop)
                        {
                            this.UpdateStopPositionInfo(positionDesc, lineCode);
                        }
                    }
                    else continue;
                }
                else
                {
                    pStatus = PositionStatus.Normal;
                }

                this.UpdatePositionLog(position, lineCode, positionType, sPositionID, fPositionID, pStatus);
            }

            try
            {
                //this.StopMusic(lineCode);
            }
            catch
            {
            }
        }

        /// <summary>
        /// Get Position Data Row
        /// </summary>
        /// <param name="position"></param>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        private DataRow GetPositionDataRow(int position, string lineCode)
        {
            if (ConfigInfo.DtAllPositions != null)
            {
                foreach (DataRow row in ConfigInfo.DtAllPositions.Rows)
                {
                    int dmPosition = 0;

                    if (int.TryParse(row["Content1"].ToString().Trim(), out dmPosition))
                    {
                        if ((position == dmPosition)
                            && string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                        {
                            return row;
                        }
                    }
                }
            }

            return null;
        }


        /// <summary>
        /// Get Position Desc
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private string GetPositionDesc(DataRow row)
        {
            if (row != null)
            {
                string positionName = row["Position"].ToString().Trim();

                return positionName;
            }

            return string.Empty;
        }


        /// <summary>
        /// Get SPositionID
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private string GetSPositionID(DataRow row)
        {
            if (row != null)
            {
                string sPositionID = row["SPositionID"].ToString().Trim();

                return sPositionID;
            }

            return string.Empty;
        }


        /// <summary>
        /// Get FPositionID
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private string GetFPositionID(DataRow row)
        {
            if (row != null)
            {
                string fPositionID = row["FPositionID"].ToString().Trim();

                return fPositionID;
            }

            return string.Empty;
        }

        /// <summary>
        /// Get Position Type
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private PositionType GetPositionType(DataRow row)
        {
            if (row != null)
            {
                if (string.Equals("1", row["Content2"].ToString().Trim()))
                {
                    return PositionType.Help;
                }
                else if (string.Equals("1", row["Content3"].ToString().Trim()))
                {
                    return PositionType.Alarm;
                }
                else if (string.Equals("1", row["Content4"].ToString().Trim()))
                {
                    return PositionType.Stop;
                }
                else
                {
                    return PositionType.Unknown;
                }
            }

            return PositionType.Null;
        }


        /// <summary>
        /// Update Help Position Info
        /// </summary>
        /// <param name="positionDesc"></param>
        /// <param name="lineCode"></param>
        private void UpdateHelpPositionInfo(string positionDesc, string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                this.hasSHAPositionLine1 = true;
                this.hasHPositionLine1 = true;

                if (string.IsNullOrWhiteSpace(this.lblPositionInfo1Line1.Text))
                {
                    this.lblPositionInfo1Line1.Text = positionDesc;
                    this.lblPositionInfo1Line1.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo2Line1.Text))
                {
                    this.lblPositionInfo2Line1.Text = positionDesc;
                    this.lblPositionInfo2Line1.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo3Line1.Text))
                {
                    this.lblPositionInfo3Line1.Text = positionDesc;
                    this.lblPositionInfo3Line1.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo4Line1.Text))
                {
                    this.lblPositionInfo4Line1.Text = positionDesc;
                    this.lblPositionInfo4Line1.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo5Line1.Text))
                {
                    this.lblPositionInfo5Line1.Text = positionDesc;
                    this.lblPositionInfo5Line1.ForeColor = System.Drawing.Color.Yellow;
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                this.hasSHAPositionLine2 = true;
                this.hasHPositionLine2 = true;

                if (string.IsNullOrWhiteSpace(this.lblPositionInfo1Line2.Text))
                {
                    this.lblPositionInfo1Line2.Text = positionDesc;
                    this.lblPositionInfo1Line2.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo2Line2.Text))
                {
                    this.lblPositionInfo2Line2.Text = positionDesc;
                    this.lblPositionInfo2Line2.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo3Line2.Text))
                {
                    this.lblPositionInfo3Line2.Text = positionDesc;
                    this.lblPositionInfo3Line2.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo4Line2.Text))
                {
                    this.lblPositionInfo4Line2.Text = positionDesc;
                    this.lblPositionInfo4Line2.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo5Line2.Text))
                {
                    this.lblPositionInfo5Line2.Text = positionDesc;
                    this.lblPositionInfo5Line2.ForeColor = System.Drawing.Color.Yellow;
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                this.hasSHAPositionLine3 = true;
                this.hasHPositionLine3 = true;

                if (string.IsNullOrWhiteSpace(this.lblPositionInfo1Line3.Text))
                {
                    this.lblPositionInfo1Line3.Text = positionDesc;
                    this.lblPositionInfo1Line3.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo2Line3.Text))
                {
                    this.lblPositionInfo2Line3.Text = positionDesc;
                    this.lblPositionInfo2Line3.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo3Line3.Text))
                {
                    this.lblPositionInfo3Line3.Text = positionDesc;
                    this.lblPositionInfo3Line3.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo4Line3.Text))
                {
                    this.lblPositionInfo4Line3.Text = positionDesc;
                    this.lblPositionInfo4Line3.ForeColor = System.Drawing.Color.Yellow;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo5Line3.Text))
                {
                    this.lblPositionInfo5Line3.Text = positionDesc;
                    this.lblPositionInfo5Line3.ForeColor = System.Drawing.Color.Yellow;
                }
            }

            this.Refresh();
            this.Update();
        }


        /// <summary>
        /// Update Alarm Position Info
        /// </summary>
        /// <param name="positionDesc"></param>
        /// <param name="lineCode"></param>
        private void UpdateAlarmPositionInfo(string positionDesc, string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                this.hasSHAPositionLine1 = true;
                this.hasSAPositionLine1 = true;

                if (string.IsNullOrWhiteSpace(this.lblPositionInfo1Line1.Text))
                {
                    this.lblPositionInfo1Line1.Text = positionDesc;
                    this.lblPositionInfo1Line1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo2Line1.Text))
                {
                    this.lblPositionInfo2Line1.Text = positionDesc;
                    this.lblPositionInfo2Line1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo3Line1.Text))
                {
                    this.lblPositionInfo3Line1.Text = positionDesc;
                    this.lblPositionInfo3Line1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo4Line1.Text))
                {
                    this.lblPositionInfo4Line1.Text = positionDesc;
                    this.lblPositionInfo4Line1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo5Line1.Text))
                {
                    this.lblPositionInfo5Line1.Text = positionDesc;
                    this.lblPositionInfo5Line1.ForeColor = System.Drawing.Color.Red;
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                this.hasSHAPositionLine2 = true;
                this.hasSAPositionLine2 = true;

                if (string.IsNullOrWhiteSpace(this.lblPositionInfo1Line2.Text))
                {
                    this.lblPositionInfo1Line2.Text = positionDesc;
                    this.lblPositionInfo1Line2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo2Line2.Text))
                {
                    this.lblPositionInfo2Line2.Text = positionDesc;
                    this.lblPositionInfo2Line2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo3Line2.Text))
                {
                    this.lblPositionInfo3Line2.Text = positionDesc;
                    this.lblPositionInfo3Line2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo4Line2.Text))
                {
                    this.lblPositionInfo4Line2.Text = positionDesc;
                    this.lblPositionInfo4Line2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo5Line2.Text))
                {
                    this.lblPositionInfo5Line2.Text = positionDesc;
                    this.lblPositionInfo5Line2.ForeColor = System.Drawing.Color.Red;
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                this.hasSHAPositionLine3 = true;
                this.hasSAPositionLine3 = true;

                if (string.IsNullOrWhiteSpace(this.lblPositionInfo1Line3.Text))
                {
                    this.lblPositionInfo1Line3.Text = positionDesc;
                    this.lblPositionInfo1Line3.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo2Line3.Text))
                {
                    this.lblPositionInfo2Line3.Text = positionDesc;
                    this.lblPositionInfo2Line3.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo3Line3.Text))
                {
                    this.lblPositionInfo3Line3.Text = positionDesc;
                    this.lblPositionInfo3Line3.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo4Line3.Text))
                {
                    this.lblPositionInfo4Line3.Text = positionDesc;
                    this.lblPositionInfo4Line3.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo5Line3.Text))
                {
                    this.lblPositionInfo5Line3.Text = positionDesc;
                    this.lblPositionInfo5Line3.ForeColor = System.Drawing.Color.Red;
                }
            }

            this.Refresh();
            this.Update();
        }

        /// <summary>
        /// Update Stop Position Info
        /// </summary>
        /// <param name="positionDesc"></param>
        /// <param name="lineCode"></param>
        private void UpdateStopPositionInfo(string positionDesc, string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                this.hasSHAPositionLine1 = true;
                this.hasSAPositionLine1 = true;

                if (string.IsNullOrWhiteSpace(this.lblPositionInfo1Line1.Text))
                {
                    this.lblPositionInfo1Line1.Text = positionDesc;
                    this.lblPositionInfo1Line1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo2Line1.Text))
                {
                    this.lblPositionInfo2Line1.Text = positionDesc;
                    this.lblPositionInfo2Line1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo3Line1.Text))
                {
                    this.lblPositionInfo3Line1.Text = positionDesc;
                    this.lblPositionInfo3Line1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo4Line1.Text))
                {
                    this.lblPositionInfo4Line1.Text = positionDesc;
                    this.lblPositionInfo4Line1.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo5Line1.Text))
                {
                    this.lblPositionInfo5Line1.Text = positionDesc;
                    this.lblPositionInfo5Line1.ForeColor = System.Drawing.Color.Red;
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                this.hasSHAPositionLine2 = true;
                this.hasSAPositionLine2 = true;

                if (string.IsNullOrWhiteSpace(this.lblPositionInfo1Line2.Text))
                {
                    this.lblPositionInfo1Line2.Text = positionDesc;
                    this.lblPositionInfo1Line2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo2Line2.Text))
                {
                    this.lblPositionInfo2Line2.Text = positionDesc;
                    this.lblPositionInfo2Line2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo3Line2.Text))
                {
                    this.lblPositionInfo3Line2.Text = positionDesc;
                    this.lblPositionInfo3Line2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo4Line2.Text))
                {
                    this.lblPositionInfo4Line2.Text = positionDesc;
                    this.lblPositionInfo4Line2.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo5Line2.Text))
                {
                    this.lblPositionInfo5Line2.Text = positionDesc;
                    this.lblPositionInfo5Line2.ForeColor = System.Drawing.Color.Red;
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                this.hasSHAPositionLine3 = true;
                this.hasSAPositionLine3 = true;

                if (string.IsNullOrWhiteSpace(this.lblPositionInfo1Line3.Text))
                {
                    this.lblPositionInfo1Line3.Text = positionDesc;
                    this.lblPositionInfo1Line3.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo2Line3.Text))
                {
                    this.lblPositionInfo2Line3.Text = positionDesc;
                    this.lblPositionInfo2Line3.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo3Line3.Text))
                {
                    this.lblPositionInfo3Line3.Text = positionDesc;
                    this.lblPositionInfo3Line3.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo4Line3.Text))
                {
                    this.lblPositionInfo4Line3.Text = positionDesc;
                    this.lblPositionInfo4Line3.ForeColor = System.Drawing.Color.Red;
                }
                else if (string.IsNullOrWhiteSpace(this.lblPositionInfo5Line3.Text))
                {
                    this.lblPositionInfo5Line3.Text = positionDesc;
                    this.lblPositionInfo5Line3.ForeColor = System.Drawing.Color.Red;
                }
            }

            this.Refresh();
            this.Update();
        }


        /// <summary>
        /// Clear Position Info
        /// </summary>
        /// <param name="lineCode"></param>
        private void ClearPositionInfo(string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                this.hasSHAPositionLine1 = false;
                this.hasHPositionLine1 = false;
                this.hasSAPositionLine1 = false;

                this.lblPositionInfo1Line1.Text = string.Empty;
                this.lblPositionInfo2Line1.Text = string.Empty;
                this.lblPositionInfo3Line1.Text = string.Empty;
                this.lblPositionInfo4Line1.Text = string.Empty;
                this.lblPositionInfo5Line1.Text = string.Empty;
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                this.hasSHAPositionLine2 = false;
                this.hasHPositionLine2 = false;
                this.hasSAPositionLine2 = false;

                this.lblPositionInfo1Line2.Text = string.Empty;
                this.lblPositionInfo2Line2.Text = string.Empty;
                this.lblPositionInfo3Line2.Text = string.Empty;
                this.lblPositionInfo4Line2.Text = string.Empty;
                this.lblPositionInfo5Line2.Text = string.Empty;
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                this.hasSHAPositionLine3 = false;
                this.hasHPositionLine3 = false;
                this.hasSAPositionLine3 = false;

                this.lblPositionInfo1Line3.Text = string.Empty;
                this.lblPositionInfo2Line3.Text = string.Empty;
                this.lblPositionInfo3Line3.Text = string.Empty;
                this.lblPositionInfo4Line3.Text = string.Empty;
                this.lblPositionInfo5Line3.Text = string.Empty;
            }
        }

        /// <summary>
        /// Has SHA Position
        /// </summary>
        /// <returns></returns>
        public bool HasSHAPosition()
        {
            if (this.hasSHAPositionLine1
                || this.hasSHAPositionLine2
                || this.hasSHAPositionLine3)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Change Shift Name and Update Product Log
        /// </summary>
        /// <returns></returns>
        private bool ChangeShiftNameAndUpdateProductLog()
        {
            bool isShiftNameChanged = false;

            string shiftInfoLine1 = string.Empty;
            string shiftInfoLine2 = string.Empty;
            string shiftInfoLine3 = string.Empty;

            if (ConfigInfo.CurrentLineCodeCount >= 1)
            {
                shiftInfoLine1 = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys1);

                if (!string.Equals(this.lblShiftLine1.Text.Trim(), shiftInfoLine1.Trim()))
                {
                    isShiftNameChanged = true;
                    this.lblCurrentPlanLine1.Text = "0";
                    this.currentNowPlanUpdatedTimeLine1 = DateTime.Now;
                }
            }

            if (ConfigInfo.CurrentLineCodeCount >= 2)
            {
                shiftInfoLine2 = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys2);

                if (!string.Equals(this.lblShiftLine2.Text.Trim(), shiftInfoLine2.Trim()))
                {
                    isShiftNameChanged = true;
                    this.lblCurrentPlanLine2.Text = "0";
                    this.currentNowPlanUpdatedTimeLine2 = DateTime.Now;
                }
            }

            if (ConfigInfo.CurrentLineCodeCount >= 3)
            {
                shiftInfoLine3 = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys3);

                if (!string.Equals(this.lblShiftLine3.Text.Trim(), shiftInfoLine3.Trim()))
                {
                    isShiftNameChanged = true;
                    this.lblCurrentPlanLine3.Text = "0";
                    this.currentNowPlanUpdatedTimeLine3 = DateTime.Now;
                }
            }

            if (isShiftNameChanged)
            {
                this.UpdateProductLog();

                this.lblShiftLine1.Text = shiftInfoLine1.Trim();
                this.lblShiftLine2.Text = shiftInfoLine2.Trim();
                this.lblShiftLine3.Text = shiftInfoLine3.Trim();
            }

            return isShiftNameChanged;
        }

        /// <summary>
        /// Get Shift
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string GetShift(string text)
        {
            if (text.Contains("1"))
            {
                return "1";
            }
            else if (text.Contains("2"))
            {
                return "2";
            }

            return "0";
        }

        /// <summary>
        /// Update Product Log
        /// </summary>
        private void UpdateProductLog()
        {
            string logDate = DateTime.Now.Date.ToString("yyyy-MM-dd");

            if (ConfigInfo.CurrentLineCodeCount >= 1)
            {
                string shift = this.GetShift(this.lblShiftLine1.Text);
                Product.UpdateProductLogByKey(ConfigInfo.LineCode1,
                                              shift,
                                              logDate,
                                              this.lblDayPlanLine1.Text,
                                              this.lblCurrentPlanLine1.Text,
                                              this.lblActualCompletedLine1.Text,
                                              this.lblStopTimeLine1.Text);
            }

            if (ConfigInfo.CurrentLineCodeCount >= 2)
            {
                string shift = this.GetShift(this.lblShiftLine2.Text);
                Product.UpdateProductLogByKey(ConfigInfo.LineCode2,
                                              shift,
                                              logDate,
                                              this.lblDayPlanLine2.Text,
                                              this.lblCurrentPlanLine2.Text,
                                              this.lblActualCompletedLine2.Text,
                                              this.lblStopTimeLine2.Text);
            }

            if (ConfigInfo.CurrentLineCodeCount >= 3)
            {
                string shift = this.GetShift(this.lblShiftLine3.Text);
                Product.UpdateProductLogByKey(ConfigInfo.LineCode3,
                                              shift,
                                              logDate,
                                              this.lblDayPlanLine3.Text,
                                              this.lblCurrentPlanLine3.Text,
                                              this.lblActualCompletedLine3.Text,
                                              this.lblStopTimeLine3.Text);
            }
        }

        /// <summary>
        /// Update Position Log
        /// </summary>
        /// <param name="position"></param>
        /// <param name="lineCode"></param>
        /// <param name="positionType"></param>
        /// <param name="sPositionID"></param>
        /// <param name="fPositionID"></param>
        /// <param name="positionStatus"></param>
        private void UpdatePositionLog(int position, string lineCode, PositionType positionType, string sPositionID, string fPositionID, PositionStatus positionStatus)
        {
            string logDate = DateTime.Now.ToString("yyyy-MM-dd");
            string logTime = DateTime.Now.ToString("HH:mm:ss");
            string shift = string.Empty;
            string type = string.Empty;
            string stopTime = string.Empty;
            string helpTime = string.Empty;
            string alarmTime = string.Empty;
            TimeSpan span = new TimeSpan(0);
            bool isNeedUpdatePositionLog = false;

            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if (positionStatus == PositionStatus.Unnormal)
                {
                    if (this.positionStatusInfoLine1.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine1[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine1[position].Status = positionStatus;
                            this.positionStatusInfoLine1[position].StatusBeginTime = DateTime.Now;
                        }
                    }
                    else
                    {
                        PositionStatusInfo positionStatusInfo = new PositionStatusInfo();
                        positionStatusInfo.Status = positionStatus;
                        positionStatusInfo.StatusBeginTime = DateTime.Now;

                        this.positionStatusInfoLine1.Add(position, positionStatusInfo);
                    }
                }
                else if (positionStatus == PositionStatus.Normal)
                {
                    if (this.positionStatusInfoLine1.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine1[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine1[position].Status = positionStatus;
                            this.positionStatusInfoLine1[position].StatusEndTime = DateTime.Now;

                            span = this.positionStatusInfoLine1[position].StatusEndTime
                                 - this.positionStatusInfoLine1[position].StatusBeginTime;

                            shift = this.GetShift(this.lblShiftLine1.Text);

                            isNeedUpdatePositionLog = true;
                        }
                    }
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if (positionStatus == PositionStatus.Unnormal)
                {
                    if (this.positionStatusInfoLine2.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine2[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine2[position].Status = positionStatus;
                            this.positionStatusInfoLine2[position].StatusBeginTime = DateTime.Now;
                        }
                    }
                    else
                    {
                        PositionStatusInfo positionStatusInfo = new PositionStatusInfo();
                        positionStatusInfo.Status = positionStatus;
                        positionStatusInfo.StatusBeginTime = DateTime.Now;

                        this.positionStatusInfoLine2.Add(position, positionStatusInfo);
                    }
                }
                else if (positionStatus == PositionStatus.Normal)
                {
                    if (this.positionStatusInfoLine2.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine2[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine2[position].Status = positionStatus;
                            this.positionStatusInfoLine2[position].StatusEndTime = DateTime.Now;

                            span = this.positionStatusInfoLine2[position].StatusEndTime
                                 - this.positionStatusInfoLine2[position].StatusBeginTime;

                            shift = this.GetShift(this.lblShiftLine2.Text);

                            isNeedUpdatePositionLog = true;
                        }
                    }
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if (positionStatus == PositionStatus.Unnormal)
                {
                    if (this.positionStatusInfoLine3.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine3[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine3[position].Status = positionStatus;
                            this.positionStatusInfoLine3[position].StatusBeginTime = DateTime.Now;
                        }
                    }
                    else
                    {
                        PositionStatusInfo positionStatusInfo = new PositionStatusInfo();
                        positionStatusInfo.Status = positionStatus;
                        positionStatusInfo.StatusBeginTime = DateTime.Now;

                        this.positionStatusInfoLine3.Add(position, positionStatusInfo);
                    }
                }
                else if (positionStatus == PositionStatus.Normal)
                {
                    if (this.positionStatusInfoLine3.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine3[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine3[position].Status = positionStatus;
                            this.positionStatusInfoLine3[position].StatusEndTime = DateTime.Now;

                            span = this.positionStatusInfoLine3[position].StatusEndTime
                                 - this.positionStatusInfoLine3[position].StatusBeginTime;

                            shift = this.GetShift(this.lblShiftLine3.Text);

                            isNeedUpdatePositionLog = true;
                        }
                    }
                }
            }

            if (isNeedUpdatePositionLog)
            {
                if ((positionType == PositionType.Help)
                    || (positionType == PositionType.Unknown))
                {
                    type = "H";
                    helpTime = span.TotalSeconds.ToString("0");
                }
                else if (positionType == PositionType.Alarm)
                {
                    type = "A";
                    alarmTime = span.TotalSeconds.ToString("0");
                }
                else if (positionType == PositionType.Stop)
                {
                    type = "S";
                    stopTime = span.TotalSeconds.ToString("0");
                }
                else
                {
                    return;
                }

                Position.UpdatePositionLogByKey(lineCode,
                                                logDate,
                                                shift,
                                                logTime,
                                                type,
                                                sPositionID,
                                                fPositionID,
                                                stopTime,
                                                helpTime,
                                                alarmTime);

                Position.UpdatePositionLogTempByKey(lineCode,
                                                    logDate,
                                                    shift,
                                                    logTime,
                                                    type,
                                                    sPositionID,
                                                    fPositionID,
                                                    stopTime,
                                                    helpTime,
                                                    alarmTime);
            }
        }

        /// <summary>
        /// Show Screen Saver
        /// </summary>
        private void ShowScreenSaver()
        {
            if (this.screenSaver == null)
            {
                this.screenSaver = new ScreenSaver();
            }

            if (this.screenSaver.IsDisposed)
            {
                this.screenSaver = new ScreenSaver();
            }

            if (!this.screenSaver.Visible)
            {
                this.screenSaver.ShowDialog();
            }
        }

        /// <summary>
        /// Stop Music
        /// </summary>
        private void StopMusic(string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                    if (!this.hasSAPositionLine1)
                    {
                        this.mediaPlayerAlarmLine1.Ctlcontrols.stop();
                        this.mediaPlayerStopLine1.Ctlcontrols.stop();
                    }

                    if (!this.hasHPositionLine1)
                    {
                        this.mediaPlayerHelpLine1.Ctlcontrols.stop();
                    }
                
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {

                    if (!this.hasSAPositionLine2)
                    {
                        this.mediaPlayerAlarmLine2.Ctlcontrols.stop();
                        this.mediaPlayerStopLine2.Ctlcontrols.stop();
                    }

                    if (!this.hasHPositionLine2)
                    {
                        this.mediaPlayerHelpLine2.Ctlcontrols.stop();
                    }
                
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {

                    if (!this.hasSAPositionLine3)
                    {
                        this.mediaPlayerAlarmLine3.Ctlcontrols.stop();
                        this.mediaPlayerStopLine3.Ctlcontrols.stop();
                    }

                    if (!this.hasHPositionLine3)
                    {
                        this.mediaPlayerHelpLine3.Ctlcontrols.stop();
                    }
                
            }
        }

        /// <summary>
        /// Stop All Music
        /// </summary>
        private void StopAllMusic()
        {
            try
            {
                this.axWindowsMediaPlayer.Ctlcontrols.stop();
                this.mediaPlayerAlarmLine1.Ctlcontrols.stop();
                this.mediaPlayerHelpLine1.Ctlcontrols.stop();
                this.mediaPlayerStopLine1.Ctlcontrols.stop();
                //this.mediaPlayerAlarmLine2.Ctlcontrols.stop();
                //this.mediaPlayerHelpLine2.Ctlcontrols.stop();
                //this.mediaPlayerStopLine2.Ctlcontrols.stop();
                //this.mediaPlayerAlarmLine3.Ctlcontrols.stop();
                //this.mediaPlayerHelpLine3.Ctlcontrols.stop();
                //this.mediaPlayerStopLine3.Ctlcontrols.stop();
            }
            catch
            {
            }
        }

        /// <summary>
        /// Play Music By Priority
        /// </summary>
        private void PlayMusicByPriority()
        {
            if (IsStopPlan1)
            {
                this.hasSAPositionLine1 = false;
                this.hasHPositionLine1 = false;
                this.IsExcep1 = false;
            }
            if (IsStopPlan2)
            {
                this.hasSAPositionLine2 = false;
                this.hasHPositionLine2= false;
                this.IsExcep2 = false;
            }
            if (IsStopPlan3)
            {
                this.hasSAPositionLine3 = false;
                this.hasHPositionLine3 = false;
                this.IsExcep3 = false;
            }
            if (this.hasSAPositionLine1)
            {
                if (!IsExcep1 && !IsExcep2 && !IsExcep3)
                {
                    this.PlayMusic(ConfigInfo.LineCode1, PositionType.Alarm);
                }
            }
            else if (this.hasSAPositionLine2)
            {
                if (!IsExcep1 && !IsExcep2 && !IsExcep3)
                {
                    this.PlayMusic(ConfigInfo.LineCode2, PositionType.Alarm);
                }

            }
            else if (this.hasSAPositionLine3)
            {
                if (!IsExcep1 && !IsExcep2 && !IsExcep3)
                {
                    this.PlayMusic(ConfigInfo.LineCode3, PositionType.Alarm);
                }
            }
            else if (this.hasHPositionLine1)
            {
                //if ((!IsExcep1 && !IsStopPlan1)&&(!IsExcep2 && !IsStopPlan2)&&(!IsExcep3 && !IsStopPlan3))

                if (!IsExcep1 && !IsExcep2 && !IsExcep3)
                {
                    this.PlayMusic(ConfigInfo.LineCode1, PositionType.Help);
                }
            }
            else if (this.hasHPositionLine2)
            {
                if (!IsExcep1 && !IsExcep2 && !IsExcep3)
                {
                    this.PlayMusic(ConfigInfo.LineCode2, PositionType.Help);
                }
            }
            else if (this.hasHPositionLine3)
            {
                if (!IsExcep1 && !IsExcep2 && !IsExcep3)
                {
                    this.PlayMusic(ConfigInfo.LineCode3, PositionType.Help);
                }
            }
            else
            {
                if (!IsExcep1 && !IsExcep2 && !IsExcep3)
                {
                    this.StopAllMusic();
                }
            }

        }


        /// <summary>
        /// Play Music
        /// </summary>
        /// <param name="lineCode"></param>
        /// <param name="positionType"></param>
        private void PlayMusic(string lineCode, PositionType positionType)
        {

            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if (!IsStopPlan1)
                {
                    if (IsExcep1)
                    {
                        //if (this.axWindowsMediaPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
                        //{
                        //    this.StopMusic(lineCode);
                        //}
                        if (positionType != PositionType.Alarm || this.axWindowsMediaPlayer != this.mediaPlayerAlarmLine1)
                        {
                            this.axWindowsMediaPlayer.close();
                        }
                    }
                }
            }
            else if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if (!IsStopPlan2)
                {
                    if (IsExcep2)
                    {
                        if (positionType != PositionType.Alarm || this.axWindowsMediaPlayer != this.mediaPlayerAlarmLine2)
                        {
                            this.axWindowsMediaPlayer.close();
                        }
                    }
                }
            }
            else if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if (!IsStopPlan1)
                {
                    if (IsExcep3)
                    {
                        if (positionType != PositionType.Alarm || this.axWindowsMediaPlayer != this.mediaPlayerAlarmLine3)
                        {
                            this.axWindowsMediaPlayer.close();
                        }
                    }
                }
            }
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if (positionType != PositionType.Help || this.axWindowsMediaPlayer != this.mediaPlayerHelpLine1)
                {
                    if (positionType != PositionType.Alarm || this.axWindowsMediaPlayer != this.mediaPlayerAlarmLine1)
                    {
                        this.axWindowsMediaPlayer.close();
                    }
                }
            }
            else if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if (positionType != PositionType.Help || this.axWindowsMediaPlayer != this.mediaPlayerHelpLine2)
                {
                    if (positionType != PositionType.Alarm || this.axWindowsMediaPlayer != this.mediaPlayerAlarmLine2)
                    {
                        this.axWindowsMediaPlayer.close();
                    }
                }
            }
            else if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if (positionType != PositionType.Help || this.axWindowsMediaPlayer != this.mediaPlayerHelpLine3)
                {
                    if (positionType != PositionType.Alarm || this.axWindowsMediaPlayer != this.mediaPlayerAlarmLine3)
                    {
                        this.axWindowsMediaPlayer.close();
                    }
                }
            }

            if (this.axWindowsMediaPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                return;
            }

            string musicName = string.Empty;

            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if ((positionType == PositionType.Help)
                    || (positionType == PositionType.Unknown))
                {
                    musicName = ConfigInfo.GetHelpMusicName(ConfigInfo.DtLineWatchSys1);

                    this.axWindowsMediaPlayer = this.mediaPlayerHelpLine1;
                }
                else if (positionType == PositionType.Alarm)
                {
                    musicName = ConfigInfo.GetAlarmMusicName(ConfigInfo.DtLineWatchSys1);

                    this.axWindowsMediaPlayer = this.mediaPlayerAlarmLine1;
                }
                else if (positionType == PositionType.Stop)
                {
                    musicName = ConfigInfo.GetStopMusicName(ConfigInfo.DtLineWatchSys1);

                    this.axWindowsMediaPlayer = this.mediaPlayerStopLine1;
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if ((positionType == PositionType.Help)
                    || (positionType == PositionType.Unknown))
                {
                    musicName = ConfigInfo.GetHelpMusicName(ConfigInfo.DtLineWatchSys2);

                    this.axWindowsMediaPlayer = this.mediaPlayerHelpLine2;
                }
                else if (positionType == PositionType.Alarm)
                {
                    musicName = ConfigInfo.GetAlarmMusicName(ConfigInfo.DtLineWatchSys2);

                    this.axWindowsMediaPlayer = this.mediaPlayerAlarmLine2;
                }
                else if (positionType == PositionType.Stop)
                {
                    musicName = ConfigInfo.GetStopMusicName(ConfigInfo.DtLineWatchSys2);

                    this.axWindowsMediaPlayer = this.mediaPlayerStopLine2;
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if ((positionType == PositionType.Help)
                    || (positionType == PositionType.Unknown))
                {
                    musicName = ConfigInfo.GetHelpMusicName(ConfigInfo.DtLineWatchSys3);

                    this.axWindowsMediaPlayer = this.mediaPlayerHelpLine3;
                }
                else if (positionType == PositionType.Alarm)
                {
                    musicName = ConfigInfo.GetAlarmMusicName(ConfigInfo.DtLineWatchSys3);

                    this.axWindowsMediaPlayer = this.mediaPlayerAlarmLine3;
                }
                else if (positionType == PositionType.Stop)
                {
                    musicName = ConfigInfo.GetStopMusicName(ConfigInfo.DtLineWatchSys3);

                    this.axWindowsMediaPlayer = this.mediaPlayerStopLine3;
                }
            }

            if (!string.IsNullOrWhiteSpace(musicName))
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory
                                + ConfigurationManager.AppSettings["UploadFileDirectory"]
                                + "/"+ lineCode + "/"
                                + musicName;

                FileInfo file = new FileInfo(filePath);
                if (file.Exists)
                {
                    try
                    {
                        this.axWindowsMediaPlayer.URL = file.FullName;
                        this.axWindowsMediaPlayer.Ctlcontrols.play();
                    }
                    catch { }
                }
            }
        }

        /// <summary>
        /// timer Tick
        /// for Screen Saver
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            this.ChangeShiftNameAndUpdateProductLog();

            int timeSpan = ConfigInfo.GetScreenTimeSpan1(ConfigInfo.DtLineWatchSys1);

            if (timeSpan <= 0)
            {
                return;
            }

            TimeSpan span = DateTime.Now - Util.PositionNormalTime;

            if (span.TotalSeconds >= timeSpan)
            {
                this.ShowScreenSaver();
            }
        }

        /// <summary>
        /// timerMusic Tick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerMusic_Tick(object sender, EventArgs e)
        {
            this.PlayMusicByPriority();
        }

        /// <summary>
        /// Qiut
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.StopAllMusic();

            try
            {
                foreach (SerialPortOperator serialPortOperator in this.lineCodesAndSerialPortOperators.Values)
                {
                    serialPortOperator.ClosePort();
                }
            }
            catch
            {

            }

            this.Close();

            Application.Exit();
        }

        /// <summary>
        /// Form Closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductManagementView_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.StopAllMusic();

            try
            {
                this.UpdateProductLog();
            }
            catch
            {

            }

            try
            {
                foreach (SerialPortOperator serialPortOperator in this.lineCodesAndSerialPortOperators.Values)
                {
                    serialPortOperator.ClosePort();
                }
            }
            catch
            {

            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            //Setup form = new Setup();
            //form.ShowDialog();

            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.csv)|*.csv";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    if (myStream == null)
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }

            LinePositionModel model = new LinePositionModel();
            model.LinePositionTable = ConfigInfo.DtAllPositions.Clone();
            DataTable tempLinePositionTable = ConfigInfo.DtAllPositions.Clone();

            //create object of CSVReader and pass the stream

            Encoding encoding = Encoding.GetEncoding(ConfigurationManager.AppSettings["CSVEncoding"]);
            CSVReader reader = new CSVReader(myStream, encoding);

            string[] data = new string[100];
            int lineIndex = 0;

            while ((data = reader.GetCSVLine()) != null)
            {
                lineIndex++;
                if (lineIndex == 1)
                {
                    continue;
                }

                if (data.Length >= 5)
                {
                    DataRow newRow = model.LinePositionTable.NewRow();

                    newRow["Line_Code"] = data[0].Trim();
                    newRow["SPositionID"] = data[1].Trim();
                    newRow["FPositionID"] = data[2].Trim();
                    newRow["Position"] = data[3].Trim();
                    newRow["SetDate"] = string.Empty;
                    newRow["User"] = string.Empty;

                    if (!string.IsNullOrWhiteSpace(data[4].Trim()))
                    {
                        newRow["Content1"] = data[4].Trim();
                        newRow["Content2"] = "1";
                        newRow["Content3"] = "0";
                        newRow["Content4"] = "0";
                    }
                    else if ((data.Length >= 6) && !string.IsNullOrWhiteSpace(data[5].Trim()))
                    {
                        newRow["Content1"] = data[5].Trim();
                        newRow["Content2"] = "0";
                        newRow["Content3"] = "1";
                        newRow["Content4"] = "0";
                    }
                    else if ((data.Length >= 7) && !string.IsNullOrWhiteSpace(data[6].Trim()))
                    {
                        newRow["Content1"] = data[6].Trim();
                        newRow["Content2"] = "0";
                        newRow["Content3"] = "0";
                        newRow["Content4"] = "1";
                    }

                    newRow["Content5"] = string.Empty;
                    newRow["Content6"] = string.Empty;
                    model.LinePositionTable.Rows.Add(newRow);
                }
            }

            // Add Existing Positions
            foreach (DataRow row in ConfigInfo.DtAllPositions.Rows)
            {
                string lineCodeTemp = row["Line_Code"].ToString().Trim();

                DataRow[] rows = model.LinePositionTable.Select("Line_Code = '" + lineCodeTemp + "'");

                if ((rows == null) || (rows.Length <= 0))
                {
                    tempLinePositionTable.ImportRow(row);
                    tempLinePositionTable.AcceptChanges();
                }
            }

            foreach (DataRow row in tempLinePositionTable.Rows)
            {
                model.LinePositionTable.ImportRow(row);
                model.LinePositionTable.AcceptChanges();
            }


            bool result = Position.SaveAllLinePositions(model);

            if (result)
            {
                ConfigInfo.InitAllPositions();
                MessageBox.Show("保存成功!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("保存失败!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (myStream != null)
            {
                myStream.Close();
            }
        }

        private void SetCommentInfo(TableLayoutPanel tlpPositionAndCommentInfo, Label lblCommentInfo1, Label lblCommentInfo2, string commentInfo1, string commentInfo2)
        {
            lblCommentInfo1.Text = commentInfo1;
            lblCommentInfo2.Text = commentInfo2;

            tlpPositionAndCommentInfo.ColumnStyles.Clear();
            tlpPositionAndCommentInfo.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
            tlpPositionAndCommentInfo.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));

        }

        private void SetPositionInfo(TableLayoutPanel tlpPositionAndCommentInfo)
        {
            tlpPositionAndCommentInfo.ColumnStyles.Clear();
            tlpPositionAndCommentInfo.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            tlpPositionAndCommentInfo.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSBGZLine1_Click(object sender, EventArgs e)
        {
            if (!this.isSBGZInfoShowLine1)
            {
                this.SetCommentInfo(this.tlpPositionAndCommentInfoLine1,
                                                   this.lblCommentInfo1Line1, this.lblCommentInfo2Line1, "设备", "故障");
                this.isSBGZInfoShowLine1 = true;
            }
            else
            {
                this.SetPositionInfo(this.tlpPositionAndCommentInfoLine1);
                this.isSBGZInfoShowLine1 = false;
            }

            this.isPZBLInfoShowLine1 = false;
            this.isBPQPInfoShowLine1 = false;
        }

        private void btnPZBLLine1_Click(object sender, EventArgs e)
        {
            if (!this.isPZBLInfoShowLine1)
            {
                this.SetCommentInfo(this.tlpPositionAndCommentInfoLine1,
                                                   this.lblCommentInfo1Line1, this.lblCommentInfo2Line1, "品质", "不良");
                this.isPZBLInfoShowLine1 = true;
            }
            else
            {
                this.SetPositionInfo(this.tlpPositionAndCommentInfoLine1);
                this.isPZBLInfoShowLine1 = false;
            }

            this.isSBGZInfoShowLine1 = false;
            this.isBPQPInfoShowLine1 = false;
        }

        private void btnBPQPLine1_Click(object sender, EventArgs e)
        {
            if (!this.isBPQPInfoShowLine1)
            {
                this.SetCommentInfo(this.tlpPositionAndCommentInfoLine1,
                                                   this.lblCommentInfo1Line1, this.lblCommentInfo2Line1, "部品", "欠品");
                this.isBPQPInfoShowLine1 = true;
            }
            else
            {
                this.SetPositionInfo(this.tlpPositionAndCommentInfoLine1);
                this.isBPQPInfoShowLine1 = false;
            }

            this.isPZBLInfoShowLine1 = false;
            this.isSBGZInfoShowLine1 = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSBGZLine2_Click(object sender, EventArgs e)
        {
            if (!this.isSBGZInfoShowLine2)
            {
                this.SetCommentInfo(this.tlpPositionAndCommentInfoLine2,
                                                   this.lblCommentInfo1Line2, this.lblCommentInfo2Line2, "设备", "故障");
                this.isSBGZInfoShowLine2 = true;
            }
            else
            {
                this.SetPositionInfo(this.tlpPositionAndCommentInfoLine2);
                this.isSBGZInfoShowLine2 = false;
            }

            this.isPZBLInfoShowLine2 = false;
            this.isBPQPInfoShowLine2 = false;
        }

        private void btnPZBLLine2_Click(object sender, EventArgs e)
        {
            if (!this.isPZBLInfoShowLine2)
            {
                this.SetCommentInfo(this.tlpPositionAndCommentInfoLine2,
                                                   this.lblCommentInfo1Line2, this.lblCommentInfo2Line2, "品质", "不良");
                this.isPZBLInfoShowLine2 = true;
            }
            else
            {
                this.SetPositionInfo(this.tlpPositionAndCommentInfoLine2);
                this.isPZBLInfoShowLine2 = false;
            }

            this.isSBGZInfoShowLine2 = false;
            this.isBPQPInfoShowLine2 = false;
        }

        private void btnBPQPLine2_Click(object sender, EventArgs e)
        {
            if (!this.isBPQPInfoShowLine2)
            {
                this.SetCommentInfo(this.tlpPositionAndCommentInfoLine2,
                                                   this.lblCommentInfo1Line2, this.lblCommentInfo2Line2, "部品", "欠品");
                this.isBPQPInfoShowLine2 = true;
            }
            else
            {
                this.SetPositionInfo(this.tlpPositionAndCommentInfoLine2);
                this.isBPQPInfoShowLine2 = false;
            }

            this.isPZBLInfoShowLine2 = false;
            this.isSBGZInfoShowLine2 = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSBGZLine3_Click(object sender, EventArgs e)
        {
            if (!this.isSBGZInfoShowLine3)
            {
                this.SetCommentInfo(this.tlpPositionAndCommentInfoLine3,
                                                   this.lblCommentInfo1Line3, this.lblCommentInfo2Line3, "设备", "故障");
                this.isSBGZInfoShowLine3 = true;
            }
            else
            {
                this.SetPositionInfo(this.tlpPositionAndCommentInfoLine3);
                this.isSBGZInfoShowLine3 = false;
            }

            this.isPZBLInfoShowLine3 = false;
            this.isBPQPInfoShowLine3 = false;
        }

        private void btnPZBLLine3_Click(object sender, EventArgs e)
        {
            if (!this.isPZBLInfoShowLine3)
            {
                this.SetCommentInfo(this.tlpPositionAndCommentInfoLine3,
                                                   this.lblCommentInfo1Line3, this.lblCommentInfo2Line3, "品质", "不良");
                this.isPZBLInfoShowLine3 = true;
            }
            else
            {
                this.SetPositionInfo(this.tlpPositionAndCommentInfoLine3);
                this.isPZBLInfoShowLine3 = false;
            }

            this.isSBGZInfoShowLine3 = false;
            this.isBPQPInfoShowLine3 = false;
        }

        private void btnBPQPLine3_Click(object sender, EventArgs e)
        {
            if (!this.isBPQPInfoShowLine3)
            {
                this.SetCommentInfo(this.tlpPositionAndCommentInfoLine3,
                                                   this.lblCommentInfo1Line3, this.lblCommentInfo2Line3, "部品", "欠品");
                this.isBPQPInfoShowLine3 = true;
            }
            else
            {
                this.SetPositionInfo(this.tlpPositionAndCommentInfoLine3);
                this.isBPQPInfoShowLine3 = false;
            }

            this.isPZBLInfoShowLine3 = false;
            this.isSBGZInfoShowLine3 = false;
        }

        private void Update_Exception(string ExcepValue, string portName)
        {
            long ExcepNumber = -1;
            long temp = 0;

            if (long.TryParse(ExcepValue, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out temp))
            {
                long.TryParse(ExcepValue, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out ExcepNumber);
            }

            string lineCode = this.serialPortsAndLineCodes[portName];

            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if (ExcepNumber > 0)
                {
                    IsExcep1 = true;
                    Excep_Show1(IsExcep1);
                }
                else
                {
                    IsExcep1 = false;
                    Excep_Show1(IsExcep1);
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if (ExcepNumber > 0)
                {
                    IsExcep2 = true;
                    Excep_Show2(IsExcep2);
                }
                else
                {
                    IsExcep2 = false;
                    Excep_Show2(IsExcep2);
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if (ExcepNumber > 0)
                {
                    IsExcep3 = true;
                    Excep_Show3(IsExcep3);
                }
                else
                {
                    IsExcep3 = false;
                    Excep_Show3(IsExcep3);
                }
            }
        }

        private void Update_StopPlan(string StopPlanValue, string portName)
        {
            long StopPlanNumber = -1;
            long temp = 0;

            if (long.TryParse(StopPlanValue, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out temp))
            {
                long.TryParse(StopPlanValue, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out StopPlanNumber);
            }
            string lineCode = this.serialPortsAndLineCodes[portName];

            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if (StopPlanNumber > 0)
                {
                    IsStopPlan1 = true;
                    StopPlan_Show1(IsStopPlan1);
                }
                else
                {
                    IsStopPlan1 = false;
                    StopPlan_Show1(IsStopPlan1);
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if (StopPlanNumber > 0)
                {
                    IsStopPlan2 = true;
                    StopPlan_Show2(IsStopPlan2);
                }
                else
                {
                    IsStopPlan2 = false;
                    StopPlan_Show2(IsStopPlan2);
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if (StopPlanNumber > 0)
                {
                    IsStopPlan3 = true;
                    StopPlan_Show3(IsStopPlan3);
                }
                else
                {
                    IsStopPlan3 = false;
                    StopPlan_Show3(IsStopPlan3);
                }
            }
        }

        private void Excep_Show1(bool IsExcep)
        {
            if (!IsStopPlan1)
            {
                if (IsExcep)
                {
                    this.SetCommentInfo(this.tlpPositionAndCommentInfoLine1,
                                                       this.lblCommentInfo1Line1, this.lblCommentInfo2Line1, "异常", "停止");
                    this.PlayMusic(ConfigInfo.LineCode1, PositionType.Alarm);
                }
                else
                {
                    this.SetPositionInfo(this.tlpPositionAndCommentInfoLine1);
                }
            }
        }

        private void StopPlan_Show1(bool IsStopPlan)
        {
            if (!IsExcep1)
            {
                if (IsStopPlan)
                {
                    this.SetCommentInfo(this.tlpPositionAndCommentInfoLine1,
                                                       this.lblCommentInfo1Line1, this.lblCommentInfo2Line1, "计划", "停止");
                    //this.StopMusic(ConfigInfo.LineCode1);
                }
                else
                {
                    this.SetPositionInfo(this.tlpPositionAndCommentInfoLine1);
                }
            }
        }

        private void Excep_Show2(bool IsExcep)
        {
            if (!IsStopPlan2)
            {
                if (IsExcep)
                {
                    this.SetCommentInfo(this.tlpPositionAndCommentInfoLine2,
                                                       this.lblCommentInfo1Line2, this.lblCommentInfo2Line2, "异常", "停止");
                    this.PlayMusic(ConfigInfo.LineCode1, PositionType.Alarm);
                }
                else
                {
                    this.SetPositionInfo(this.tlpPositionAndCommentInfoLine2);
                }
            }
        }

        private void StopPlan_Show2(bool IsStopPlan)
        {
            if (!IsExcep2)
            {
                if (IsStopPlan)
                {
                    this.SetCommentInfo(this.tlpPositionAndCommentInfoLine2,
                                                       this.lblCommentInfo1Line2, this.lblCommentInfo2Line2, "计划", "停止");
                    //this.StopMusic(ConfigInfo.LineCode2);
                }
                else
                {
                    this.SetPositionInfo(this.tlpPositionAndCommentInfoLine2);
                }
            }
        }

        private void Excep_Show3(bool IsExcep)
        {
            if (!IsStopPlan3)
            {
                if (IsExcep)
                {
                    this.SetCommentInfo(this.tlpPositionAndCommentInfoLine3,
                                                       this.lblCommentInfo1Line3, this.lblCommentInfo2Line3, "异常", "停止");
                    this.PlayMusic(ConfigInfo.LineCode1, PositionType.Alarm);
                }
                else
                {
                    this.SetPositionInfo(this.tlpPositionAndCommentInfoLine3);
                }
            }
        }

        private void StopPlan_Show3(bool IsStopPlan)
        {
            if (!IsExcep3)
            {
                if (IsStopPlan)
                {
                    this.SetCommentInfo(this.tlpPositionAndCommentInfoLine3,
                                                       this.lblCommentInfo1Line3, this.lblCommentInfo2Line3, "计划", "停止");
                    //this.StopMusic(ConfigInfo.LineCode3);
                }
                else
                {
                    this.SetPositionInfo(this.tlpPositionAndCommentInfoLine3);
                }
            }
        }

        private void butExcep1_Click(object sender, EventArgs e)
        {
            //if (!IsExcep1)
            //{
            //    this.SetCommentInfo(this.tlpPositionAndCommentInfoLine1,
            //                                       this.lblCommentInfo1Line1, this.lblCommentInfo2Line1, "异常", "停止");
            //    IsExcep1 = true;
            //}
            //else
            //{
            //    this.SetPositionInfo(this.tlpPositionAndCommentInfoLine3);
            //    IsExcep1 = false;
            //}
            //IsStopPlan1 = false;
        }

        private void butStopPlan1_Click(object sender, EventArgs e)
        {
            //if (!IsStopPlan1)
            //{
            //    this.SetCommentInfo(this.tlpPositionAndCommentInfoLine1,
            //                                       this.lblCommentInfo1Line1, this.lblCommentInfo2Line1, "异常", "停止");
            //    IsStopPlan1 = true;
            //}
            //else
            //{
            //    this.SetPositionInfo(this.tlpPositionAndCommentInfoLine3);
            //    IsStopPlan1 = false;
            //}
            //IsExcep1 = false;
        }
    }
}
